package com.caishi.lkx.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.caishi.lkx.user.mapper.*;
import com.caishi.lkx.user.model.ISysUserLog;
import com.caishi.lkx.user.service.IUserLogService;
import com.zzw.common.threadpool.SysThreadPool;
import com.zzw.common.entity.IBSMapper;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author by keray
 * date:2021/4/20 11:07 上午
 */
@Service
public class UserLogServiceImpl implements IUserLogService {

    @Resource
    private SysUserLogMapper sysUserLogMapper;

    @Resource
    private SysUserLogAdminLoginMapper sysUserLogAdminLoginMapper;

    @Resource
    private SysUserLogTeacherLoginMapper sysUserLogTeacherLoginMapper;

    @Resource
    private SysUserLogMemberLoginMapper sysUserLogMemberLoginMapper;

    @Resource
    private SysUserLogGroupAdminLoginMapper sysUserLogGroupAdminLoginMapper;

    @Resource
    private SysUserLogGroupMemberLoginMapper sysUserLogGroupMemberLoginMapper;

    @Resource
    private SysUserLogAgentLoginMapper sysUserLogAgentLoginMapper;

    @Override
    public IBSMapper<? extends ISysUserLog<?>, Long> routerMapper(ISysUserLog.SysUserLog superModel) {
        String type = superModel.getLogType();
        if (ISysUserLog.ADMIN_LOGIN.equals(type)) {
            return sysUserLogAdminLoginMapper;
        } else if (ISysUserLog.TEACHER_LOGIN.equals(type)) {
            return sysUserLogTeacherLoginMapper;
        } else if (ISysUserLog.MEMBER_LOGIN.equals(type)) {
            return sysUserLogMemberLoginMapper;
        } else if (ISysUserLog.GROUP_ADMIN_LOGIN.equals(type)) {
            return sysUserLogGroupAdminLoginMapper;
        } else if (ISysUserLog.GROUP_MEMBER_LOGIN.equals(type)) {
            return sysUserLogGroupMemberLoginMapper;
        } else if (ISysUserLog.AGENT_LOGIN.equals(type)) {
            return sysUserLogAgentLoginMapper;
        }
        return sysUserLogMapper;
    }

    @Override
    public ISysUserLog<?> routerEntity(ISysUserLog.SysUserLog superModel) {
        ISysUserLog<?> model = superModel;
        String type = superModel.getLogType();
        if (ISysUserLog.ADMIN_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogAdminLogin();
        } else if (ISysUserLog.TEACHER_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogTeacherLogin();
        } else if (ISysUserLog.MEMBER_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogMemberLogin();
        } else if (ISysUserLog.GROUP_ADMIN_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogGroupAdminLogin();
        } else if (ISysUserLog.GROUP_MEMBER_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogGroupMemberLogin();
        } else if (ISysUserLog.AGENT_LOGIN.equals(type)) {
            model = new ISysUserLog.SysUserLogAgentLogin();
        } else {
            return model;
        }
        BeanUtil.copyProperties(superModel, model);
        return model;
    }

    @Override
    public ISysUserLog.SysUserLog routerEntityTrans(ISysUserLog<?> entity) {
        var k = new ISysUserLog.SysUserLog();
        BeanUtil.copyProperties(entity, k);
        return k;
    }

    @Override
    public void putLog(ISysUserLog.SysUserLog log) {
        SysThreadPool.execute(() -> this.insert(log));
    }
}
